package;

import board.Dice;
import board.Roulette;
import board.Team;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.system.FlxSound;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxMath;
import flixel.input.gamepad.LogitechButtonID;
import flixel.input.gamepad.PS4ButtonID;
import flixel.util.FlxColor;
import haxe.Utf8;
import flixel.tweens.FlxTween;
import flixel.tweens.FlxEase;
import sys.db.Connection;
import sys.db.SqLite;

/**
 * A FlxState which can be used for the actual gameplay.
 */
class IntroState extends FlxState
{
	var text1:FlxText;
	var text2:FlxText;
	var text3:FlxText;
	var start:FlxText;
	var nuevaPartida:FlxText;
	var continuar:FlxText;
	var logo:FlxSprite;
	var blinking:Bool;
	var time:Float;
	var reset:Float;
	var sound:FlxSound;
	var estado:String;
	var seleccion:Int;
	var rondasSeleccion:Int;
	var rondasText:FlxText;
	var rondas:Int;
	/**
	 * Function that is called up when to state is created to set it up. 
	 */
	override public function create():Void
	{
		super.create();
		sound = new FlxSound();
		sound.loadEmbedded("assets/sounds/Intro.wav",true);
		sound.fadeIn(2);
		sound.play();
		FlxG.camera.fade(FlxColor.BLACK,1,true,onFadeIn);
		FlxG.camera.bgColor = FlxColor.ROYAL_BLUE;
		text1 = new FlxText(0, 20, 800, "EL JUEGO");
		text1.setFormat("title", 140, FlxColor.GOLDENROD, "center", FlxText.BORDER_OUTLINE);
		add(text1);
		text1.x = -800;
		text2 = new FlxText(0, 130, 800, "DEL");
		text2.setFormat("title", 140, FlxColor.GOLDENROD, "center", FlxText.BORDER_OUTLINE);
		add(text2);
		text2.scale.set(0, 0);
		text3 = new FlxText(0, 240, 800, "SABER");
		text3.setFormat("title", 140, FlxColor.GOLDENROD, "center", FlxText.BORDER_OUTLINE);
		add(text3);
		text3.alpha = 0;
		
		start = new FlxText(0, 400, 800, "PRESIONE ENTER");
		start.setFormat("title", 60, FlxColor.GOLDENROD, "center", FlxText.BORDER_OUTLINE);
		add(start);
		start.visible = false;
		blinking = false;
		time = 0.8;
		reset = 22;
		
		nuevaPartida = new FlxText(0, 400, 800, "> NUEVA PARTIDA");
		nuevaPartida.setFormat("title", 70, FlxColor.GOLDENROD, "center", FlxText.BORDER_OUTLINE);
		add(nuevaPartida);
		nuevaPartida.visible = false;
		
		continuar = new FlxText(0, 470, 800, "CONTINUAR PARTIDA");
		continuar.setFormat("title", 60, FlxColor.GOLDENROD, "center", FlxText.BORDER_OUTLINE);
		add(continuar);
		continuar.visible = false;
		
		
		
		logo = new FlxSprite(0, 0);
		logo.loadGraphic("assets/images/logo.png");
		logo.scale.set(0.3,0.3);
		logo.x = FlxG.width / 2 - logo.width / 2;
		logo.y = FlxG.height - 170;
		add(logo);
		estado = "esperando";
		
		seleccion = 1;
		rondasSeleccion = 1;
		rondas = 5;
		
		rondasText = new FlxText(0, 400, 800, "");
		rondasText.text = "CANTIDAD DE RONDAS:" + rondas + " (APROX: "+ rondas+ " MIN)";
		rondasText.setFormat("title", 55, FlxColor.GOLDENROD, "center", FlxText.BORDER_OUTLINE);
		add(rondasText);
		rondasText.visible = false;
		
	}
	
	private function onFadeIn():Void
	{
		var tween = FlxTween.tween(text1,{x:0},1,{ease:FlxEase.elasticOut,complete:endTween});
	}
	
	private function endTween(tween2:FlxTween):Void
	{
		var tween = FlxTween.tween(text2.scale,{x:1,y:1},1,{ease:FlxEase.bounceOut,complete:endTween2});
	}
	
	private function endTween2(tween2:FlxTween):Void
	{
		var tween = FlxTween.tween(text3,{alpha:1},1,{complete:endTween3});
	}
	
	private function endTween3(tween2:FlxTween):Void
	{
		blinking = true;
		time = 0.8;
	}
	
	/**
	 * Function that is called when this state is destroyed - you might want to 
	 * consider setting all objects this state uses to null to help garbage collection.
	 */
	override public function destroy():Void
	{
		super.destroy();
	}

	/**
	 * Function that is called once every frame.
	 */
	override public function update():Void
	{
		super.update();
		switch(estado)
		{
			case "esperando":
				time -= FlxG.elapsed;
				reset -= FlxG.elapsed;
				if (FlxG.keys.justPressed.ENTER)
				{
					start.visible = false;
					estado = "eligiendo";
					nuevaPartida.visible = true;
					continuar.visible = true;
					return;
				}
				if (blinking && time < 0)
				{
					start.visible = !start.visible;
					time = 0.8;
				}
				if (reset < 0)
				{
					sound.fadeOut();
					FlxG.camera.fade(FlxColor.BLACK,1,false,onFade);
				}
			case "eligiendo":
				if ((FlxG.keys.justPressed.UP || FlxG.keys.justPressed.DOWN)  && seleccion == 1 )
				{
					seleccion = 2;
					nuevaPartida.text = "NUEVA PARTIDA";
					nuevaPartida.size = 60;
					continuar.text = "> CONTINUAR PARTIDA";
					continuar.size = 62;
				}
				else if ((FlxG.keys.justPressed.UP || FlxG.keys.justPressed.DOWN)  && seleccion == 2 )
				{
					seleccion = 1;
					nuevaPartida.text = "> NUEVA PARTIDA";
					nuevaPartida.size = 62;
					continuar.text = "CONTINUAR PARTIDA";
					continuar.size = 60;
				}
				if (FlxG.keys.justPressed.ENTER)
				{
					if (seleccion == 2)
					{
						FlxG.camera.fade(FlxColor.BLACK, 1, false, onFade2);
						sound.fadeOut();
					}
					else
					{
						nuevaPartida.visible = false;
						continuar.visible = false;
						estado = "rondas";
						rondasText.visible = true;
					}
				}
			case "rondas":
				if (FlxG.keys.justPressed.RIGHT)
				{
					rondas += 5;
					if (rondas == 105)
						rondas = 100;
					rondasText.text = "CANTIDAD DE RONDAS:" + rondas + " (APROX: "+ rondas+ " MIN)";
				}
				if (FlxG.keys.justPressed.LEFT)
				{
					rondas -= 5;
					if (rondas == 0)
						rondas = 5;
					rondasText.text = "CANTIDAD DE RONDAS:" + rondas + " (APROX: "+ rondas+ " MIN)";
				}
				if (FlxG.keys.justPressed.ENTER)
				{
					FlxG.camera.fade(FlxColor.BLACK, 1, false, onFade2);
					sound.fadeOut();
				}
				
		}
	}	
	
	private function onFade():Void
	{
			FlxG.switchState(new LogoState());
			sound.stop();
	}
	
	private function onFade2():Void
	{
			if (seleccion == 1)
			{
				Reg.rondas = rondas;
				var cnx = sys.db.Sqlite.open("assets/TestDB.sqlite");
				var rset = cnx.request("DELETE FROM Partidas");
				try 
				{
					cnx.request("INSERT INTO Partidas (score1,score2,score3,score4,carcel1,carcel2,carcel3,carcel4,multiplier1,multiplier2,multiplier3,multiplier4,posicion1,posicion2,posicion3,posicion4,rondas) VALUES (" +
					Reg.score1 + "," +  Reg.score2 + "," + Reg.score3 + "," + Reg.score4 + "," +
					Reg.carcel1 + "," + Reg.carcel2 + "," + Reg.carcel3 + "," + Reg.carcel4 + "," +
					Reg.multiplier1 + "," + Reg.multiplier2 + "," + Reg.multiplier3 + "," + Reg.multiplier4 + "," +
					Reg.position1 + "," + Reg.position2 + "," + Reg.position3 + "," + Reg.position4 + "," + Reg.rondas + ")");
				} 
				catch( msg : String ) {
					trace("Error occurred: " + msg);
				}
				var rset = cnx.request("DELETE FROM ScoreAdd");
				try 
				{
					cnx.request("INSERT INTO ScoreAdd (add1,add2,add3,add4) VALUES (" +
					Reg.scoreAdd1 + "," +  Reg.scoreAdd2 + "," + Reg.scoreAdd3 + "," + Reg.scoreAdd4 + ")");
				} 
				catch( msg : String ) {
					trace("Error occurred: " + msg);
				}
				cnx.close();
			}
			FlxG.switchState(new MenuState());
			sound.stop();
	}
}